#pragma once
//http应用层？
#include"TcpServer.hpp"//传输层协议？
#include"ThreadPool.hpp"
#include"Protocol.hpp"
#include"Task.hpp"
static const uint16_t defaultport=8888;

class HttpServer
{
    private:
    bool quit_;
    uint16_t port_;
    Sock*listenfd;
    public:
    HttpServer(const uint16_t port=defaultport)
    :quit_(true),port_(port)
    {

    }
    void Init()
    {
        //忽略sigpipe信号： 读端关闭，写端继续
        signal(SIGPIPE,SIG_IGN);
    }
    void Loop()
    {
        TcpServer*tcp=TcpServer::GetInstance(port_);
        listenfd=tcp->Getsock();

        quit_=false;
        while(!quit_)
        {
            uint16_t peerport;
            std::string peerip;
           int sockfd= listenfd->Accept(&peerport,&peerip);
           if(sockfd<0)continue;
           lg(INFO,"accept success sockfd is:%d",sockfd);
             //把任务push到线程池里面，让线程池处理任务
             Task t(sockfd);
         ThreadPool::GetInstance()->Push(t);
        
        // pthread_t rtd;
        // pthread_create(&rtd,nullptr,Entrance::HandlerRequest,&sockfd);
        // pthread_detach(rtd);//线程分离
        }
        quit_=true;
      
    }
    ~HttpServer()
    {

    }
};